function saveArticleBeingWritten()
{
    var signature_element = document.getElementById("article_custom_signature");
    var abstract_element = document.getElementById("article_abstract");
    var timestamp_element = document.getElementById("article_timestamp");
    
    
	var params = "article_idarticle="+encodeURIComponent(document.getElementById("article_idarticle").value)
			+"&"+"article_title="+encodeURIComponent(document.getElementById("article_title").value)
			+"&"+"article_content="+encodeURIComponent(document.getElementById("article_content").value)
            +"&"+"article_signature_style="+encodeURIComponent(document.getElementById("signature_type_selection").value);
	
    if(signature_element != null)
    {
        params += "&article_signature="+encodeURIComponent(signature_element.value);
    }    
    if(abstract_element != null)
    {
        params += "&article_abstract="+encodeURIComponent(abstract_element.value);
    }    
    if(timestamp_element != null)
    {
        params += "&article_timestamp="+encodeURIComponent(timestamp_element.value);
    }
    
	var url= "write/save/";
	var resp = postReq(url,params);
	
	var id = parseInt(resp.slice(-6));
	
	document.getElementById("article_idarticle").value = id; 
}

function defineAbstract()
{
	document.getElementById("p_article_abstract").innerHTML='Abstract:<BR/><textarea id="article_abstract" name="abstract" rows="5" style="width: 90%;"/>';
}
function defineTimestamp()
{
	document.getElementById("p_article_timestamp").innerHTML=
		'Article timestamp: <input name="article_timestamp"  id="article_timestamp" value=""/>'+
		'<input type=button value="select" onclick="displayDatePicker(\'article_timestamp\');"/>';
	displayDatePicker('article_timestamp');
}

function checkArticle()
{
    return document.getElementById("article_title").value && (document.getElementById("article_title").value != "NaN");
}

function add_tag()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_tag",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding tags.")
    }
}

function search_tags_button_pressed()
{
    var criteria = document.getElementById("search_tag").value;
    search_tags(criteria, false);    
}

function search_tags(criteria, show_all, offset)
{
    var dlg;
    if(offset == null)
    {
        offset = 0;
    }
    var params = "offset="+offset+"&article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
    if(criteria)
    {
        params += "&criteria="+encodeURIComponent(criteria);
    }
     dlg = postReq("write/add_tag",params);    
    popup(dlg);    
}



function addThisTag(id)
{
    redirect('write/add_this_tag/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

function remove_tag(id)
{
    redirect('write/remove_this_tag/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

function create_new_tag()
{
    redirect('write/create_new_tag/' + encodeURIComponent(document.getElementById("new_tag_name").value) + "/" + encodeURIComponent(document.getElementById("article_idarticle").value));    
}

///////////////////////////////////////////////////////////////////////////////////
// People tagging

function add_peopletag()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_peopletag",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding tags.")
    }
}

function search_peopletags_button_pressed()
{
    var criteria = document.getElementById("search_peopletag").value;
    search_peopletags(criteria, false);    
}

function search_peopletags(criteria, show_all, offset)
{
    var dlg;
    if(offset == null)
    {
        offset = 0;
    }
    var params = "offset="+offset+"&article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
    if(criteria)
    {
        params += "&criteria="+encodeURIComponent(criteria);
    }
    dlg = postReq("write/add_peopletag",params);    
    popup(dlg);    
}



function addThisPeopleTag(id)
{
    redirect('write/add_this_peopletag/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

function remove_peopletag(id)
{
    redirect('write/remove_this_peopletag/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

function create_new_peopletag()
{
    redirect('write/create_new_peopletag/' + encodeURIComponent(document.getElementById("new_peopletag_name").value) + "/" + encodeURIComponent(document.getElementById("article_idarticle").value));    
}



///////////////////////////////////////////////////////////////////////////////////

function add_geostamp()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_geostamp",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding geostamps.")
    }
}

function search_geostamps_button_pressed()
{
    var criteria = document.getElementById("search_geostamps").value;
    search_geostamps(criteria, false);    
}

function search_geostamps(criteria, show_all, offset)
{
    var dlg;
    if(offset == null)
    {
        offset = 0;
    }
    var params = "offset="+offset+"&article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
    if(criteria)
    {
        params += "&criteria="+encodeURIComponent(criteria);
    }
     dlg = postReq("write/add_geostamp",params);    
    popup(dlg);    
}



function addThisGeostamp(id)
{
    redirect('write/add_this_geostamp/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

function remove_geostamp(id)
{
    redirect('write/remove_this_geostamp/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

///////////////////////////////////////////////////////////////////////////////////////////
// Signature 

function define_custom_signature()
{
	document.getElementById("p_article_signature").innerHTML='Signature:<BR/><textarea id="article_signature" name="article_signature" rows="1" cols="45"/>';
}

function signature_type_changed()
{
   s_input = document.getElementById("custom_signature")
   if(document.getElementById("signature_type_selection").value == 3)
   {
       s_input.style.visibility = "visible";
   }
   else
   {
       s_input.style.visibility = "hidden";
   }
}

///////////////////////////////////////////////////////////////////////////////////////////
// Add links for the see also section

function add_link()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_links",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding links.")
    }
}

function add_new_link()
{
    var url = document.getElementById("new_link_url").value;
    var text = document.getElementById("new_link_text").value;
    
    var params = "link_url="+url+"&link_text="+text+
        "&article_id="+document.getElementById("article_idarticle").value;
    
    postReq("write/add_new_link",params);
    redirect('write/edit/' + encodeURIComponent(document.getElementById("article_idarticle").value));    
}

function remove_link(id)
{
    redirect('write/remove_this_link/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id);    
}

///////////////////////////////////////////////////////////////////////////////////////////
// Uploading a document

function add_document()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_document",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding links.")
    }
}

///////////////////////////////////////////////////////////////////////////////////////////
// Add articles for the see also these articles section - and add to the story

function add_related_article()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_related_article",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding links.")
    }
}

function add_new_related_article(id)
{
    //var article_relation_type = encodeURIComponent(document.getElementById("relation_type").value);
    
    var params = "article_relation_type=0"+
        "&related_article_id="+id+
        "&article_id="+document.getElementById("article_idarticle").value;
    
    postReq("write/add_new_related_article",params);
    redirect('write/edit/' + encodeURIComponent(document.getElementById("article_idarticle").value));   
}

function remove_related_article(id_to_remove)
{
    redirect('write/remove_related_article/' + encodeURIComponent(document.getElementById("article_idarticle").value) + "/"+ id_to_remove);    
}

function search_articles_button_pressed(for_story)
{
    var criteria = document.getElementById("search_articles").value;
    search_articles(criteria, false, 0, for_story);    
}

function search_articles(criteria, show_all, offset, for_story)
{
    var dlg;
    if(offset == null)
    {
        offset = 0;
    }
    var params = "offset="+offset+"&article_id="+document.getElementById("article_idarticle").value;
    if(criteria)
    {
        params += "&criteria="+encodeURIComponent(criteria);
    }
    if(!for_story)
    {
        dlg = postReq("write/add_related_article",params);    
    }
    else
    {
        dlg = postReq("write/add_to_story",params);
    }
    popup(dlg);    
}

function add_to_story()
{
    if(checkArticle())
    {
        saveArticleBeingWritten();
        var params = "article_id="+encodeURIComponent(document.getElementById("article_idarticle").value);
        
        var dlg = postReq("write/add_to_story",params);
        popup(dlg);
    }
    else
    {
        message_box("Input at least title before adding links.")
    }
}

function add_to_this_story(id)
{
    var params = "related_article_id="+id+
        "&article_id="+document.getElementById("article_idarticle").value;
    
    postReq("write/add_to_this_story",params);
    redirect('write/edit/' + encodeURIComponent(document.getElementById("article_idarticle").value));   
}